package GUI;

import item.*;
import item.Item.Category;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class GUI_page03_Shopping extends JPanel {
	Boolean cameFromLogin;
	List<ShoppingCartItem> shoppingList;
	String cid;
	
	JPanel paneLeft = new JPanel();
	JLabel titleLabel;
	JButton logoutButton;
	
	JLabel categoryLabel;
	List<String> categoryOptions;
	JComboBox categoryList;
	JLabel itemTitleLabel;
	JTextField itemTitleField;
	JLabel leadSingerLabel;
	JTextField leadSingerField;
	
	JTable table;
	JScrollPane scrollPane;
	
	JPanel addPanel = new JPanel();
	JLabel quantityLabel;
	JTextField quantityField;
	JButton addToCartButton;
	
	JPanel paneRight = new JPanel();
	JLabel titleLabelRight;
	JList shoppingCart;
	JScrollPane shoppingCartScroller;
	DefaultListModel listModel;
	JButton removeItemButton;
	
	JButton checkoutButton;
	
	
	
	public GUI_page03_Shopping() {
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.setBackground(Color.white);
		
		// get number of categories and names
		categoryOptions = new ArrayList<String>();
		for (Category cat : Category.values())
			categoryOptions.add(cat.toString());

		
		// for search stuff
		paneLeft.setLayout(new BoxLayout(paneLeft, BoxLayout.Y_AXIS));
		paneLeft.setAlignmentX(LEFT_ALIGNMENT);
		paneLeft.setMaximumSize(new Dimension(400, 450));
		paneLeft.setBackground(Color.white);
		this.add(paneLeft);
		
		this.add(Box.createRigidArea(new Dimension(50,0)));
		
		// for cart stuff
		paneRight.setLayout(new BoxLayout(paneRight, BoxLayout.Y_AXIS));
		paneRight.setAlignmentX(LEFT_ALIGNMENT);
		paneRight.setMaximumSize(new Dimension(400, 450));
		paneRight.setBackground(Color.WHITE);
		this.add(paneRight);
		
		// LEFT PANEL
		
		// title label
		titleLabel = new JLabel("Shopping Menu", JLabel.CENTER);
		titleLabel.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabel.setForeground(Color.gray);
        paneLeft.add(titleLabel);
        
        paneLeft.add(Box.createRigidArea(new Dimension(0,20)));

        // logout button
        logoutButton = GUI_AMSStoreMain.makeGrayWhiteButton("Logout", Component.LEFT_ALIGNMENT);
        paneLeft.add(logoutButton);
        
        paneLeft.add(Box.createRigidArea(new Dimension(0,15)));
        
        // category drop down
		categoryLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Category", 11);
		paneLeft.add(categoryLabel);
		// list of categories to choose from, by default on first one
        Object[] tableStrings = categoryOptions.toArray();
        categoryList = new JComboBox(tableStrings);
        categoryList.setSelectedIndex(0);
        categoryList.setAlignmentX(Component.LEFT_ALIGNMENT);
        categoryList.setForeground(Color.gray);
        categoryList.setBackground(Color.white);
        categoryList.setFont(new Font("Century Gothic", Font.PLAIN, 13));
        categoryList.setMaximumSize(new Dimension(150,25));
        categoryList.setSize(new Dimension(150, 25)); 
        paneLeft.add(categoryList);
		
		paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		// title field
		itemTitleLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Title", 11);
		paneLeft.add(itemTitleLabel);
		itemTitleField = GUI_AMSStoreMain.makeLeftGrayTextField("", 110);
		paneLeft.add(itemTitleField);
		
		paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		// lead singer field
		leadSingerLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Lead Singer", 11);
		paneLeft.add(leadSingerLabel);
		leadSingerField = GUI_AMSStoreMain.makeLeftGrayTextField("", 100);
		paneLeft.add(leadSingerField);
		
		paneLeft.add(Box.createRigidArea(new Dimension(0,5)));

		// results table
		scrollPane = new JScrollPane(table);
		paneLeft.add(scrollPane);
		
		paneLeft.add(Box.createRigidArea(new Dimension(0,5)));
		
		// quantity field and add to cart button (and their pane)
		addPanel.setLayout(new BoxLayout(addPanel, BoxLayout.X_AXIS));
		addPanel.setAlignmentX(LEFT_ALIGNMENT);
		addPanel.setMaximumSize(new Dimension(400, 40));
		addPanel.setBackground(Color.white);
		paneLeft.add(addPanel);
		quantityLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Amount", 11);
		addPanel.add(quantityLabel);
		addPanel.add(Box.createRigidArea(new Dimension(2,0)));
		quantityField = GUI_AMSStoreMain.makeLeftGrayTextField("1", 70);
		addPanel.add(quantityField);
		addPanel.add(Box.createRigidArea(new Dimension(10,0)));
		addToCartButton = GUI_AMSStoreMain.makeGrayWhiteButton("Add to Cart", Component.LEFT_ALIGNMENT);
		addToCartButton.setEnabled(false);
		addPanel.add(addToCartButton);
		
		// RIGHT PANEL
		
		paneRight.add(Box.createRigidArea(new Dimension(0,20)));
		
		// cart label
		titleLabelRight = new JLabel("Cart", JLabel.CENTER);
		titleLabelRight.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabelRight.setHorizontalTextPosition(JLabel.CENTER);
		titleLabelRight.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabelRight.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabelRight.setForeground(Color.gray);
        paneRight.add(titleLabelRight);
        
        paneRight.add(Box.createRigidArea(new Dimension(0,10)));
        
        // shopping cart list
        listModel = new DefaultListModel();
		listModel.addElement("Empty");
		shoppingCart = new JList(listModel);
		shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingCart.setLayoutOrientation(JList.VERTICAL);
		shoppingCart.setVisibleRowCount(-1);
		shoppingCart.setSelectedIndex(0);
		shoppingCartScroller = new JScrollPane(shoppingCart);
		shoppingCartScroller.setPreferredSize(new Dimension(250, 150));
		paneRight.add(shoppingCartScroller);
		
		paneRight.add(Box.createRigidArea(new Dimension(0,5)));
		
		// remove from cart button
        removeItemButton = GUI_AMSStoreMain.makeGrayWhiteButton("Remove Selection", Component.LEFT_ALIGNMENT);
        removeItemButton.setEnabled(false);
        paneRight.add(removeItemButton);
        
        paneRight.add(Box.createRigidArea(new Dimension(0,100)));
        
        // check out button
        checkoutButton = GUI_AMSStoreMain.makeGrayWhiteButton("Checkout", Component.LEFT_ALIGNMENT);
        checkoutButton.setForeground(Color.RED);
        checkoutButton.setEnabled(false);
        paneRight.add(checkoutButton);
	}
	
	public void resetPage()
	{
		this.populateTable(null, null);
		this.checkoutButton.setEnabled(false);
		this.addToCartButton.setEnabled(false);
		this.removeItemButton.setEnabled(false);
		
		this.itemTitleField.setText("");
		this.leadSingerField.setText("");
		this.quantityField.setText("1");
		
		shoppingList = new ArrayList<ShoppingCartItem>();
		paneRight.remove(shoppingCartScroller);
		
		listModel = new DefaultListModel();
		listModel.addElement("Empty");
		shoppingCart = new JList(listModel);
		shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingCart.setLayoutOrientation(JList.VERTICAL);
		shoppingCart.setVisibleRowCount(-1);
		shoppingCart.setSelectedIndex(0);
		shoppingCartScroller = new JScrollPane(shoppingCart);
		shoppingCartScroller.setPreferredSize(new Dimension(250, 150));
		paneRight.add(shoppingCartScroller,2);
	}
	
	public void populateTable(Object[] columnNames, Object[][] data){
		paneLeft.remove(scrollPane);
		
		if (columnNames == null || data == null)
			scrollPane = new JScrollPane();
		else
		{
			table = new JTable(data, columnNames);
			table.setPreferredScrollableViewportSize(new Dimension(400,70));
			table.setFillsViewportHeight(true);
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			scrollPane = new JScrollPane(table);
		}
		paneLeft.add(scrollPane, 13);
	}
}
